package com.weds.frame.core.advice;

import com.weds.frame.common.entity.Result;
import com.weds.frame.core.exception.AuthenticationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/3/15 16:00
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionAdivce {

    /**
     * 运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生运行时异常.", requestURI, e);
        return Result.error(e.getMessage());
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
                                                      HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
        return Result.error(e.getMessage());
    }


    /**
     * 全局捕获401异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(AuthenticationException.class)
    public Result authenticationException(Exception e) {
        if (e instanceof AuthenticationException) {
            return Result.authError(StringUtils.isEmpty(e.getMessage()) ? "验证失败" : e.getMessage());
        }
        return Result.error(StringUtils.isEmpty(e.getMessage()) ? "系统异常" : e.getMessage());
    }


    /**
     * 全局捕获hibernate字段验证异常
     *
     * @param exception
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidException(MethodArgumentNotValidException exception) {
        BindingResult result = exception.getBindingResult();
        StringBuilder stringBuilder = new StringBuilder();
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            if (errors != null) {
                errors.forEach(p -> {
                    FieldError fieldError = (FieldError) p;
                    stringBuilder.append("字段:[" + fieldError.getField() + "]" + fieldError.getDefaultMessage() + ",");
                });
            }
        }
        return Result.fieldError(stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1));
    }


//    private <T> JsonResult<T> message(Integer code, String msg, T data) {
//        JsonResult<T> jsonResult = new JsonResult();
//        jsonResult.setCode(code);
//        jsonResult.setMsg(msg);
//        jsonResult.setData(data);
//        return jsonResult;
//    }
}
